------------------------------------------------------------------------------------------------------------------------------------------------------
--[[

    targetpos  目标传送坐标，没有判定地形

]]--
------------------------------------------------------------------------------------------------------------------------------------------------------
--- server 
AddStategraphState("wilson", State{
    name = "hutao_sg_spurt",
    tags = { "aoe", "doing", "busy", "nopredict", "nomorph" },
    onenter = function(inst, data)
        data = data or {}
        local targetpos = data.targetpos    --- 传送最终坐标
        if targetpos == nil then
            inst.sg:GoToState("idle", true)
            return
        end
        ---------------------------------------------------------------------------------
        --- 刷一次坐标
            local new_tar_pos = Vector3(targetpos.x, targetpos.y, targetpos.z)
            targetpos = new_tar_pos
        ---------------------------------------------------------------------------------
        ---
        ---------------------------------------------------------------------------------
        inst.AnimState:PlayAnimation("lunge_pst")
        inst.SoundEmitter:PlaySound("dontstarve/wilson/attack_weapon")
        local pos = inst:GetPosition()
        local dir
        if pos.x ~= data.targetpos.x or pos.z ~= data.targetpos.z then
            dir = inst:GetAngleToPoint(data.targetpos)
            inst.Transform:SetRotation(dir)
        end

        inst.SoundEmitter:PlaySound("meta3/wigfrid/spear_lighting_lunge")
        --Make sure we don't land directly on world boundary, where
        --physics may end up popping in the wrong direction to void
        local x, z = data.targetpos.x, data.targetpos.z
        -- if dir then
        --     local theta = dir * DEGREES
        --     local cos_theta = math.cos(theta)
        --     local sin_theta = math.sin(theta)
        --     local x1, z1
        --     local map = TheWorld.Map
        --     if not map:IsPassableAtPoint(x, 0, z) then
        --         --scan for nearby land in case we were slightly off
        --         --adjust position slightly toward valid ground
        --         if map:IsPassableAtPoint(x + 0.1 * cos_theta, 0, z - 0.1 * sin_theta) then
        --             x1 = x + 0.5 * cos_theta
        --             z1 = z - 0.5 * sin_theta
        --         elseif map:IsPassableAtPoint(x - 0.1 * cos_theta, 0, z + 0.1 * sin_theta) then
        --             x1 = x - 0.5 * cos_theta
        --             z1 = z + 0.5 * sin_theta
        --         end
        --     else
        --         --scan to make sure we're not just on the edge of land, could result in popping to the wrong side
        --         --adjust position slightly away from invalid ground
        --         if not map:IsPassableAtPoint(x + 0.1 * cos_theta, 0, z - 0.1 * sin_theta) then
        --             x1 = x - 0.4 * cos_theta
        --             z1 = z + 0.4 * sin_theta
        --         elseif not map:IsPassableAtPoint(x - 0.1 * cos_theta, 0, z + 0.1 * sin_theta) then
        --             x1 = x + 0.4 * cos_theta
        --             z1 = z - 0.4 * sin_theta
        --         end
        --     end

        --     if x1 and map:IsPassableAtPoint(x1, 0, z1) then
        --         x, z = x1, z1
        --     end
        -- end

        --V2C: -physics doesn't resolve correctly if we teleport from
        --      one point colliding with world to another point still
        --      colliding with world.
        --     -#HACK use mass change to force physics refresh.
        local mass = inst.Physics:GetMass()
        if mass > 0 then
            inst.sg.statemem.restoremass = mass
            inst.Physics:SetMass(mass + 1)
        end
        if inst.components.playercontroller then
            inst.components.playercontroller:RemotePausePrediction(5)
        end
        inst.Physics:Teleport(x, 0, z)

        -- aoeweapon_lunge:DoLunge can get us out of the state!
        -- And then, if onexit is run before this: bugs!
        if not data.skipflash and inst.sg.currentstate == "combat_lunge" then
            inst.components.bloomer:PushBloom("lunge", "shaders/anim.ksh", -2)
            inst.components.colouradder:PushColour("lunge", 1, 1, 0, 0)
            inst.sg.statemem.flash = 1
        end

        local fx = SpawnPrefab("spear_wathgrithr_lightning_lunge_fx")
        fx.Transform:SetPosition(targetpos:Get())
        fx.Transform:SetRotation(inst:GetRotation())
            
        
    end,

    onupdate = function(inst)
        if inst.sg.statemem.flash and inst.sg.statemem.flash > 0 then
            inst.sg.statemem.flash = math.max(0, inst.sg.statemem.flash - .1)
            inst.components.colouradder:PushColour("lunge", inst.sg.statemem.flash, inst.sg.statemem.flash, 0, 0)
        end
    end,

    timeline =
    {
        FrameEvent(8, function(inst)
            if inst.sg.statemem.restoremass ~= nil then
                inst.Physics:SetMass(inst.sg.statemem.restoremass)
                inst.sg.statemem.restoremass = nil
            end
        end),
        TimeEvent(12 * FRAMES, function(inst)
            inst.components.bloomer:PopBloom("lunge")
        end),
    },

    events =
    {
        EventHandler("animover", function(inst)
            if inst.AnimState:AnimDone() then
                inst.sg:GoToState("idle")
            end
        end),
    },

    onexit = function(inst)
        if inst.sg.statemem.restoremass ~= nil then
            inst.Physics:SetMass(inst.sg.statemem.restoremass)
        end
        inst.components.bloomer:PopBloom("lunge")
        inst.components.colouradder:PopColour("lunge")
    end,
})

